import scala.swing.Frame
import scala.collection.mutable.HashMap
import scala.swing._
import scala.swing.event.ValueChanged
import scala.swing.event.ButtonClicked
import scala.swing.event.ActionEvent

package layoutVariety
{
	class MainWindow 
	{
		protected var frame = new MainFrame	
		frame.visible = false
		
		protected var mainBox : Box = null
		
		private var widgets = new HashMap[Int,Widget]()
				
		
		/**
		 *  Interface to the host language
		 */
		
		// map widget names to widgets
		protected val widgetsByName = HashMap[String,Widget]()
		
		type Handler = ()=>Unit
		
        // adds a handler to a specific UI component
        def addHandler(event: String, widgetName : String, handler: Handler)
        {
        	widgetsByName(widgetName).addHandler(event, handler)
        }
        
        // adds a widget to the widget map
        def addWidget(name: String, widget : Widget) 
        { 
          widgetsByName(name) = widget          
        }
        
    	def setWidgetAttribute(attributeName : String, widgetName: String, attributeValue : AnyRef)
    	{
		  widgetsByName(widgetName).setAttByName(attributeName,attributeValue)
		}
        
		def setMainBox(b:Widget)
		{
		  mainBox = b.asInstanceOf[Box];
		  frame.contents = b.getWidget
		}
		
		def addWidget(ID:Int,w:Widget)
		{
			widgets.put(ID,w)
			mainBox.addWidget(w)			
		}
		
		def removeWidget(ID:Int)
		{
			widgets.remove(ID)	
		}
		
		def setSize(size:(Int,Int))
		{
			frame.maximumSize = new Dimension(size._1,size._2)
			frame.minimumSize = new Dimension(size._1,size._2)
		}
		
		def maximize() = frame.maximize()
		
		def minimize() = frame.unmaximize()
		
		def setVisible() = frame.visible = true;
		
		def setInvisible() = frame.visible = false
		
		
		/*//functions to access all attributes
		def isEnabled(ID:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.isEnabled
			}
			else false
		}
		
		def setEnabled(ID:Int,v:Boolean) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setEnabled(v)
			}
		}
		def getBackground(ID:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.getBackground
			}
			else -1
		}
		
		def setBackground(ID:Int,bg:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setBackground(bg)
			}
		}
		
		def getForeground(ID:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.getForeground
			}
			else -1
		}
		
		def setForeground(ID:Int,bg:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setForeground(bg)
			}
		}
		
		def setSize(ID:Int,wi:Int,hi:Int)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setSize(wi,hi)
			}
		}
		
		def setImage(ID:Int,path:String)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setImage(path)
			}
		}
		
		def setText(ID:Int,text:String)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setText(text)
			}
		}
		
		def getText(ID:Int):String = 
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.getText
			}
			else null
		}
		
		def setFont(ID:Int,name:String,size:Int,style:Int)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setFont(name,size,style)
			}
		}
		
		def setVertical(ID:Int,top0_middle1_bottom2:Int)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.setVertical(top0_middle1_bottom2)
			}
		}
		
		def isChecked(ID:Int):Boolean =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.isChecked
			}
			else false
		}
		
		def check(ID:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				 w.check
			}
		}
		
		def unCheck(ID:Int) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				 w.unCheck
			}
		}
		
		def addItem(ID:Int,item:String) =
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				 w.addItem(item)
			}
		}
		
		def getSelected(ID:Int):String = 
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.getSelected
			}
			else null
		}
		
		def getMax(ID:Int):Int = 
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.getMax
			}
			else return -1
			
		}
		def getMin(ID:Int):Int =   
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.getMin
			}
			return -1
			
		}
		
		def setMin(ID:Int,m:Int)  
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.setMin(m)
			}
			
		}
		
		def setMax(ID:Int,m:Int)  
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.setMax(m)
			}		
		}
		
		def setVal(ID:Int,v:Int)  
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.setVal(v)
			}		
		}
		def getVal(ID:Int):Int = 
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				return w.getVal
			}
			return -1
		}
		
		//functions that configure listeners
		def whenClicked(ID:Int,callback:()=> Unit)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.whenClicked(callback)
			}		
		}
		
		def whenChanged(ID:Int,callback:()=> Unit)
		{
			var w = widgets.get(ID)
			
			if (w != null)
			{
				w.whenChanged(callback)
			}		
		}*/
	
		
	}
}